package com.sky.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    // 注入mapper
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 新增菜品
     * @param dishDTO
     * @return
     * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        log.info("添加菜品：{}",dish);
        //设置停售
        dish.setStatus(StatusConstant.DISABLE);
        //向菜品表插入数据
        dishMapper.insert(dish);
        //获取菜品数据id并插入口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            long id = dish.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    // 菜品管理分页查询
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
        log.info("分页查询");
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        BeanUtils.copyProperties(pageQueryDTO,pageQueryDTO);
        Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    // 批量删除菜品
    @Transactional
    @Override
    public void deleteDishBatch(List<Long> ids) {
        //判断当前菜品是否在售卖中
        for (Long id : ids) {
            Dish dish = dishMapper.queryById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品正在售卖中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //先判断当前菜品是否关联了套餐
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        log.info("删除菜品：{}",ids);
        dishMapper.deleteByIds(ids);
        //删除菜品关联的口味
        log.info("删除菜品关联的口味：{}",ids);
        dishFlavorMapper.deleteByDishIds(ids);

    }


    // 根据id查询菜品
    @Override
    public DishVO getById(Long id) {
        //先查询菜品数据
        log.info("查询菜品：{}",id);
        Dish dish = dishMapper.queryById(id);
        if(dish == null){
            return null;
        }
        log.info("查询菜品关联的口味：{}",id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorMapper.getByDishId(id));
        return dishVO;
    }

    // 修改菜品状态
    @Override
    public void startOrStop(Integer status, Long id) {
        //创建一个dish对象用于封装数据
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

    // 根据类型id查询菜品列表
    @Override
    public List<Dish> list(Long categoryId) {
        List<Dish> dishList = dishMapper.queryByCategoryId(categoryId);
        return dishList;
    }

    // 根据类型id查询菜品列表  user 要加上口味
    @Override
    public List<DishVO> queryByCategoryId(Long categoryId) {
        List<Dish> dishList= dishMapper.queryByCategoryId(categoryId);
        List<DishVO> dishVOList =new ArrayList<>();
        //属性拷贝
        for (Dish dish : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            log.info("查询菜品关联的口味：{}",dish.getId());
            dishVO.setFlavors(dishFlavorMapper.getByDishId(dish.getId()));
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }


    // 修改菜品
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        //修改菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //设置停售
        dish.setStatus(StatusConstant.DISABLE);
        dishMapper.update(dish);
        //删除菜品关联的口味
        log.info("删除菜品关联的口味：{}",dishDTO.getId());
        dishFlavorMapper.deleteByDishIds(List.of(dishDTO.getId()));
        //重新插入菜品关联的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            long id = dish.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }




}
